{% extends "../base.html" %}

{% block title %} {% end %}
{% block css %} {% end %}
{% block body %}
    <div class="layui-body">
    {% include _form.html %}
    </div>
{% end %}

{% block javascript %}
<script src="{{ static_url('js/jsencrypt.js') }}" charset="utf-8"></script>
<script type="text/javascript">
layui.use(['form'], function() {
    var $ = layui.jquery, form = layui.form

    form.on('checkbox(userPermission)', function(data) {
        var child = $(data.elem).parent('dt').siblings('dd').find('input')
        /* 自动选中父节点 */
        var check_parent = function (id) {
            var self = $('.role-list-form input[value="'+id+'"]')
            var pid = self.attr('data-pid') || '';
            self.prop('checked', true)
            if (pid == '') {
                return false;
            }
            check_parent(pid)
        };
        /* 自动选中子节点 */
        child.each(function(index, item) {
            item.checked = data.elem.checked;
        })
        check_parent($(data.elem).attr('data-pid'))
        form.render('checkbox')
    })

    /* 权限赋值 */
    if (formData) {
        for(var i in formData['permission']) {
            $('.role-list-form input[value="'+formData['permission'][i]+'"]').prop('checked', true)
        }
        form.render('checkbox')
    }

    // 表单验证
    form.verify({
        username : [/[A-Za-z0-9\u4e00-\u9fa5]{2,40}$/, '用户名必须2到40位字母、数字、汉字组合'],
        password : [/(.+){8,40}$/, '8到40个任意字符'],
        number : [/^[0-9]*$/, '必须输入数字啊']
    })
    form.on('submit(formSubmit)', function(obj) {
        layer.msg('数据提交中...',{time:50000})

        if (rsa_encrypt=='1' && obj.field.password) {
            // Encrypt with the public key...
            var encrypt = new JSEncrypt()
            encrypt.setPublicKey(public_key)
            var ciphertext = encrypt.encrypt(obj.field.password)
            obj.field.password = ciphertext
        }

        var permission = []
        $("input:checkbox[name='permission']:checked").each(function() {
            permission.push($(this).val())
        });
        obj.field.permission = permission

        $.ajax({
            type: "POST",
            url: obj.form.action,
            data: obj.field,
            success: function(res) {
                if (res.code==0) {
                    layer.msg('添加成功', {icon: 1, time: 2000}, function(){
                        // 父页面刷新
                        parent.location.reload();
                    })
                } else if(res.msg) {
                    layer.msg(res.msg)
                    $(":input[name='username']").focus()
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            },
            error: function(xhr){
                console.log(xhr.responseJSON)
                if (xhr.responseJSON && xhr.responseJSON.msg) {
                    layer.msg(xhr.responseJSON.msg)
                } else {
                    layer.msg('{{ _('未知错误') }}')
                }
            }
        })
        return false;
    })
})
</script>
 {% end %}
